// $ID$
/*
	Copyright (c) 2011, Yury Y. Kuznetsov
	All rights reserved.

	Redistribution and use in source and binary forms, with or without modification, 
	are permitted provided that the following conditions are met:

	o Redistributions of source code must retain the above copyright notice, 
	  this list of conditions and the following disclaimer.
	o Redistributions in binary form must reproduce the above copyright notice, 
	  this list of conditions and the following disclaimer in the documentation 
	  and/or other materials provided with the distribution.

	THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
	EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
	OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
	SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
	INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 
	TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 
	OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY 
	WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include <iostream>
#include "cgetopt.h"
#include "profiler.h"

#include <omp.h>

using namespace std;

typedef double data_t;

void
matrix_print(data_t *matr, size_t n_col, const char *title = 0);

data_t *matr0, *matr1;
size_t n_col;
bool verbose = 0;

void
usage(const char *me)
{
	cerr << "Usage: " << me << " [-h][-v] n-rows"<< endl;
	exit(EXIT_FAILURE);
}

void
matrix_generate(data_t *matr, size_t n_col)
{
	size_t nn = n_col * n_col;
	for(size_t i = 0 ; i < nn ; ++i)
		*matr++ = static_cast<data_t>(i);
}

void
matrix_zero(data_t *matr, size_t n_col)
{
	size_t nn = n_col * n_col;
	for(size_t i = 0 ; i < nn ; ++i)
		*matr++ = static_cast<data_t>(0);
}

void
matrix_print(data_t *matr, size_t n_col, const char *title)
{
	cerr << "----------------" << endl;
	for(size_t i = 0 ; i < n_col ; ++i){
		for(size_t j = 0 ; j < n_col ; ++j)
			cerr << *matr++ << " " ;
		cerr << endl;
	}

}

void
quick_test()
{
#	pragma omp parallel for
	for(int i = 0 ; i < 10 ; ++i) {
		std::cout << "i = " << i << " thread_num == " << omp_get_thread_num() << std::endl;
	}
}

int 
main(int argc, char **argv)
{

	quick_test();
	return 0;

	Getopt g;
	int c;
	const char *me = argv[0];

	while((c = g.getopt(argc, argv, "hv")) != -1) switch(c){
	case 'v':
		verbose = 1;
		break;
	case 'h':
	case '?':
	default:
		usage(me);
	}
	argc -= g.optind;
	argv += g.optind;

	if(argc != 1)
		usage(me);

	n_col = atoi(argv[0]);

	cerr << "n_col == " << n_col << endl;

	matr0 = new data_t[n_col * n_col];
	matr1 = new data_t[n_col * n_col];

	matrix_generate(matr0, n_col);
	if(verbose)
		matrix_print(matr0, n_col, "Matrix");
	//	matrix_zero(matr1, n_col);

	data_t *rp = matr1;
	data_t *col_p, *col_p_end = matr0 + n_col, 
		*row_p, *row_p_end = matr0 + n_col * n_col;

	yyk::Profiler p;

	p.start();
	for(row_p = matr0 ; row_p != row_p_end ; row_p += n_col){
		data_t *inn_p1_end = row_p + n_col;
		for(col_p = matr0 ; col_p != col_p_end ; col_p += 1){
			data_t *inn_p1 = row_p, *inn_p2 = col_p; 
			data_t s = 0;
			while(inn_p1 != inn_p1_end){
				s += *inn_p1 * *inn_p2;
				++inn_p1;
				inn_p2 += n_col;
			}
			*rp++ = s;
		}
	}
	p.stop();

	if(verbose)
		matrix_print(matr1, n_col, "Result");
	cout << "CHK: matr[" << n_col -1 << "," 
		<< n_col -1 << "] = " << matr1[n_col*n_col - 1] << endl;
	std::cout << "Elapsed: " << p.elapsed() / 1000.0 << " sec." << std::endl;

	delete [] matr1; matr1 = 0;
	delete [] matr0; matr0 = 0;

	return 0;
}

// Local Variables:
// mode: c++
// c-basic-offset: 4
// tab-width: 4
// End:
